/*
**NCS dev fsm head file for controlling the dev state
**
**This module provide the interface of dev(pp,sig,rx,rcst) fsm
**The state of dev change to other state by the current state
**and event of receiving.
**
**Copyright (C) 2016-1 andisat <www.andisat.com>
**
**Author: andisat
**Date:2016-1
**
*/

#ifndef __NCS_FSM__
#define __NCS_FSM__

#include "ncs_serv.h"
#include "ncs_dev.h"
#include "ncs_cfg.h"

/*RCST EVENT*/
enum ENUM_RCST_FSM_EVENT{
    RCST_FSM_EVENT_NULL=0,
    RECEIVE_RCST_LOGON_REQ,
    RCST_AUTH_SUCCESS,
    RCST_AUTH_FAILURE,
    RECEIVE_RCST_RES_REQ,
    RCST_RESALLOC_SUCCESS,
    RCST_RESALLOC_FAILURE,
    RECEIVE_RCST_RELRES_REQ,
    RECEIVE_NMS_LOGOFF_REQ,
    RECEIVE_RCST_LOGOFF_REQ,
    RECEIVE_NMS_RELRCSTRES_REQ,
    RECEIVE_RXEMS_CLOSERCSTTX_REQ,
    RECEIVE_RCST_RELRES_ACK,
    RECEIVE_RXEMS_OPENRXPATH_SUCCESS,
    RECEIVE_RCST_LOGOFF_ACK,
    POLL_RCST_OFFLINE,
    RECEIVE_NMS_LOCKTXPATH_REQ,
    RECEIVE_RCST_LOCK_ACK,
    RECEIVE_NMS_LOCKREL_REQ,
    RECEIVE_RCST_LOCKREL_ACK,
    RECEIVE_RCST_FAULT,
    RECEIVE_NMS_PARUPDATERCST_REQ,
    RECEIVE_RCST_BOD_REQ,
    RECEIVE_RCST_BODREL_REQ,
    RCST_FSM_EVENT_MAX
};

/*RCST FSM STATE*/
enum RCST_FSM_STATE{
    RCST_FSM_STATE_NULL=0,
    RCST_OFFLINE,
    RCST_ONLINE,
    RCST_ONLINE_OPENRX,
    RCST_ACTIVE,
    RCST_ONLINE_RELRES,
    RCST_OFFLINE_RELRES,
    //RCST_LOCK,
    //RCST_LOCK_RELRES,
    RCST_FSM_STATE_MAX
};

enum ENUM_RX_FSM_EVENT{
    RXEMS_FSM_EVENT_NULL=0,
    RECEIVE_RXEMS_LOGON_REQ,                //Receive_RxEms_Logon_Req
    RXEMS_AUTH_SUCCESS,                         //RxEms_Auth_Success
    RXEMS_AUTH_FAILURE,                         //RxEms_Auth_Failure
    POLL_RXEMS_OFFLINE,                         //Poll_RxEms_Offline
    RECEIVE_RCST_OPENRXPATH_REQ,        //Receive_Rcst_OpenRxpath_Req
    RECEIVE_RXEMS_OPENRXPATH_ACK,       //Receive_RxEms_OpenRxpath_ACK
    RECEIVE_RCST_CLOSERXPATH_REQ,        //Receive_Rcst_CloseRxpath_Req
    RECEIVE_RX_CLOSERXPATH_ACK,      //Receive_RxEms_CloseRxpath_ACK
    //RECEIVE_NMS_DELRX_REQ,                      //Receive_Nms_Delrx_Req
    RECEIVE_NMS_PARUPDATERX_REQ,          //Receive_Parupdate_Req
    RECEIVE_NMS_RELRESRX_REQ,                //Receive_Nms_Relres_Req
    RECEIVE_NMS_LOCKRXPATH_REQ,            //Receive_NMS_LockRxpath_Req
    RECEIVE_RX_LOCKRXPATH_ACK,        //Receive_RxEms_LockRxpath_Ack
    RECEIVE_NMS_LOCKRELRXPATH_REQ,      //Receive_NMS_LockrelRxpath_Req
    RECEIVE_RX_LOCKRELRXPATH_ACK,    //Receive_RxEms_LockrelRxpath_Ack
    RXEMS_FSM_EVENT_MAX
};

enum ENUM_SIG_FSM_EVENT{
    SIGEMS_FSM_EVENT_NULL=0,
    RECEIVE_SIGEMS_LOGON_REQ,    //Receive_SigEms_Logon_Req,
    SIGEMS_AUTH_SUCCESS,             //SigEms_Auth_Success
    SIGEMS_AUTH_FAILURE,
    POLL_SIGEMS_OFFLINE,              //Poll_SigEms_Offline
    RECEIVE_NMS_PARUPDATESIG_REQ,//Receive_Nms_ParupdateSig_Req
    RSIGEMS_FSM_EVENT_MAX
};

enum ENUM_PP_FSM_EVENT{
    PP_FSM_EVENT_NULL=0,
    RECEIVE_PP_LOGON_REQ,
    PP_AUTH_SUCCESS,            
    PP_AUTH_FAILURE,
    POLL_PP_OFFLINE,              
    RECEIVE_NMS_PARUPDATEPP_REQ,
    PP_FSM_EVENT_MAX
};
    
enum DEV_STATE{
    FSM_STATE_NULL=0,
    OFFLINE,
    ONLINE,
    FSM_STATE_MAX
};

#pragma pack(1)
typedef struct DEV_FSM_STATE
{
	int (*func)(struct ncs_object *ncs,struct dev_obj *dev,void *arg,int len);
	int n_state;
}FSM_STATE;
#pragma pack()

FSM_STATE PP_FSM[FSM_STATE_MAX-1][PP_FSM_EVENT_MAX-1];
FSM_STATE SIG_FSM[FSM_STATE_MAX-1][RSIGEMS_FSM_EVENT_MAX-1];
FSM_STATE RX_FSM[FSM_STATE_MAX-1][RXEMS_FSM_EVENT_MAX-1];
FSM_STATE RCST_FSM[RCST_FSM_STATE_MAX-1][RCST_FSM_EVENT_MAX-1];

typedef int (*FSM_CALLFUNC)(struct ncs_object *ncs,struct dev_obj *dev,void *arg,int len);

int pp_fsm_change(struct dev_obj *pp,char event);
int rcst_fsm_change(struct dev_obj *rcst,char event);
int rx_fsm_change(struct dev_obj *rx,char event);
int sig_fsm_change(struct dev_obj *sig,char event);
int ncs_getres_by_rcst(struct _STR_CHANNEL_ID_ *ptxcha,struct _STR_CHANNEL_ID_ *prxcha,struct _STR_RESOURCE_ *presource);
int ncs_closetx_by_rxcha(struct ncs_object *ncs,struct _STR_CHANNEL_ID_ *prxcha);
int ncs_relres_by_rcst(struct _STR_CHANNEL_ID_ *pchaId);
int ncs_getrcst_by_rx(struct _STR_CHANNEL_ID_ *prxcha,struct _STR_CHANNEL_ID_ *ptxcha);
int ncs_reallocres_by_rcst(struct _STR_CHANNEL_ID_ *ptxcha,struct _STR_CHANNEL_ID_ *pnewrxcha,struct _STR_CHANNEL_ID_ *poldrxcha);

#endif
